package com.yhj.reggie.controller;

import com.yhj.reggie.common.R;
import com.yhj.reggie.dto.SetmealDto;
import com.yhj.reggie.entity.Page;
import com.yhj.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired(required = false)
    private SetmealService setmealService;
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto, HttpSession session){
        Long id = (Long) session.getAttribute("employee");
        setmealDto.setCreateUser(id);
        setmealDto.setUpdateUser(id);
        setmealService.save(setmealDto);
        return R.success("添加成功");
    }

    @GetMapping("/page")
    public R<Page<SetmealDto>> findAll(Integer page,Integer pageSize,String name){
        Page<SetmealDto> resultPage = setmealService.findAll(page, pageSize, name);
        return R.success(resultPage);
    }

    @DeleteMapping
    public R<String> deleteByIds(@RequestParam List<Long> ids,HttpSession session){
        Long id = (Long) session.getAttribute("employee");
        setmealService.deleteById(ids,id);
        return R.success("删除成功");
    }
}
